package com.luckmgr.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.service.BaseService;
import com.common.utils.ArithUtil;
import com.common.utils.MD5;
import com.common.utils.PropsLoader;
import com.common.utils.StringUtils;
import com.luckmgr.constant.LuckmgrConstant;
import com.luckmgr.dao.ActivityLevelMapper;
import com.luckmgr.dao.LuckUserMapper;
import com.luckmgr.entity.db.ActivityLevel;
import com.luckmgr.entity.db.CountUser;
import com.luckmgr.entity.db.LuckUser;
import com.luckmgr.entity.db.User;
import com.luckmgr.service.LuckUserService;
import com.luckmgr.vo.BetRecord;


@Service
public class LuckUserServiceImpl extends BaseService<LuckUser> implements LuckUserService {
	
	@Autowired 
	ActivityLevelMapper activityLevelMapper;
	
	@Autowired
	private PropsLoader propsLoader;
	
	
	LuckUserMapper luckUserMapper;
	
	
	@Resource(name="luckUserMapper")
    public void initCompanyMapper(LuckUserMapper luckUserMapper) {
		this.luckUserMapper = luckUserMapper;
        super.setBaseDao(luckUserMapper);
    }
	
	@Override
	public int betRecord(User user,Integer activityid, 
			Map<String, List<String[]>> map) {
		List<BetRecord> bets = conversionRecord(map);
		List<ActivityLevel> list = activityLevelMapper.findLevelByAid(activityid);
		if(list == null) return -1;
		List<LuckUser> lucklist = new ArrayList<LuckUser>();
		
		for(BetRecord t : bets){
			LuckUser luckUser = findUserLevel(t, list);
			if(null != luckUser){
				luckUser.setOperatorName(user.getUserName());
				luckUser.setOperatorIp(user.getLoginIp());
				lucklist.add(luckUser);
			}
		}
		if(0!=lucklist.size()){
			luckUserMapper.deleteAll(activityid,lucklist);
			luckUserMapper.insertAll(lucklist);
		}
		return 1;
	}
	
	public LuckUser findUserLevel(BetRecord t,List<ActivityLevel> list){
		for(ActivityLevel activityLevel : list){
			if(0!=activityLevel.getMaxMoney() ){
				if(ArithUtil.compareTo((double)activityLevel.getMinMoney(), t.getMoney()) == 1) continue;
				if(ArithUtil.compareTo((double)activityLevel.getMaxMoney(), t.getMoney()) == -1) continue;
				if(null != activityLevel.getMinBetMoney() && 0 !=activityLevel.getMinBetMoney()){
					if(ArithUtil.compareTo((double)activityLevel.getMinBetMoney(), t.getBetMoney()) == 1) continue;
				}
				if(null != activityLevel.getMaxBetMoney() && 0 !=activityLevel.getMaxBetMoney()){
					if(ArithUtil.compareTo((double)activityLevel.getMaxBetMoney(), t.getBetMoney()) == -1) continue;
				}
				if(null != activityLevel.getMultBetMoney() && 0 !=activityLevel.getMultBetMoney()){
					if(t.getBetMoney() == 0) continue;
					if(ArithUtil.compareTo((t.getMoney()*activityLevel.getMultBetMoney()), t.getBetMoney()) == 1) continue;
				}
				
				LuckUser luckUser = new LuckUser();
				luckUser.setUserName(t.getUserName());
				luckUser.setEncryption(MD5.getMD5(t.getUserName()));
				luckUser.setActivityId(activityLevel.getActivityId());
				luckUser.setLevelId(activityLevel.getId());
				luckUser.setActivityName(activityLevel.getActivityName());
				luckUser.setLevelName(activityLevel.getLevelName());
				luckUser.setMoney(t.getMoney());
				luckUser.setBetMoney(t.getBetMoney());
				luckUser.setUseNum(0);
				luckUser.setUserValid(1);
				return luckUser;
			}
		}
		return null;
	}

	
	private List<BetRecord> conversionRecord(Map<String, List<String[]>> map){
		Map<String,BetRecord> rmap = new HashMap<String,BetRecord>();
		Iterator<Entry<String, List<String[]>>> iterator = map.entrySet().iterator();
		String indexkey =  propsLoader.props.getProperty(LuckmgrConstant.TZTOOL_EXCEL_USERKEY,"");
		String moneykey =  propsLoader.props.getProperty(LuckmgrConstant.TZTOOL_EXCEL_MONEY,"");
		String betmoneykey =  propsLoader.props.getProperty(LuckmgrConstant.TZTOOL_EXCEL_BETMONEY,"");
		while (iterator.hasNext()) {
			Entry<String, List<String[]>> entry = iterator.next();
			List<String[]> list = entry.getValue();
			if(list==null || list.size()==0) continue;
			int index = -1;
			int money = -1;
			int betmoney = -1;
			
			String[] haedstr = list.remove(0);
			for(int i=0;i<haedstr.length;i++){
				if(indexkey.equals(haedstr[i])){
					index = i;
				}else if(moneykey.equals(haedstr[i])){
					money = i;
				}else if(betmoneykey.equals(haedstr[i])){
					betmoney = i;
				}
			}
			if(index == -1) continue;
			
			for(String[] strs : list){
				if(strs.length==haedstr.length){
					String username = strs[index];
					username = username.trim();
					if(null == username || "".equals(username)) continue;
					BetRecord tzbet = rmap.get(username);
					if(null == tzbet){
						tzbet = new BetRecord();
						tzbet.setUserName(username);
						rmap.put(username, tzbet)	;
					}
					
					if(money!= -1){
						Double d = StringUtils.findDouble(strs[money]);
						if(null != d){
							tzbet.setMoney(ArithUtil.add(tzbet.getMoney(),d));
						}
					}
					if(betmoney!= -1){
						Double d = StringUtils.findDouble(strs[betmoney]);
						if(null != d){
							tzbet.setBetMoney(ArithUtil.add(tzbet.getBetMoney(),d));
						}
					}
				}
				
					
			}
			
		}
		return new ArrayList<BetRecord>(rmap.values());
	}

	@Override
	public List<CountUser> selectCountUser(Integer aid) {
		return luckUserMapper.selectCountUser(aid);
	}

}